<?php


namespace app\manage\controller;

use other\MessageSend;
use other\SmsMuSend;
use other\SmsWYSend;
use think\Db;
use think\Log;

class Message extends Base
{
    private $msg_type=[
        1=>'系统消息',
        2=>'右下角弹窗',
        3=>'居中强弹窗',
        4=>'短信消息',
    ];

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        // 查询条件start
        $msg_type = input('msg_type','');
        $times = input('times','');
        $phone = input('phone','');
        $realname = input('realname','');
        $status = input('status','');
        $aid = input('aid','');

        $map['role_type'] = 2;

        if(!empty($aid)){
            if($aid!='all'){
                $map['a_id']=$aid;
            }
        }
        if($this->group_id!=1){
            $map['a_id']=$this->uid;
        }else{
            if(!empty($phone)){
                $aId=model('Admin')->where('phone',$phone)->value('id');
                if($aId){
                    $map['a_id']=$aId;
                }else{
                    $map['a_id']=0;
                }
            }
            if(!empty($realname)){
                $aId=model('Admin')->where('realname',$realname)->value('id');
                if($aId){
                    $map['a_id']=$aId;
                }else{
                    $map['a_id']=0;
                }
            }
        }

        if(!empty($msg_type)){
            $map['msg_type']=$msg_type;
        }

        if($status!=''){
            $map['a.status']=$status;
        }

        if($times != ''){
            $time_arr = explode(' - ',$times);
            $map['a.create_time'] = ['between',[strtotime($time_arr[0]),strtotime($time_arr[1])]];
        }

        $infos = model('MsgNotice')->alias('a')->field('a.*,b.phone,b.realname')
            ->join('admin b','b.id=a.a_id')
            ->where($map)->order('a.id desc')->paginate();

        foreach ($infos as &$v){
            $v['msg_type']=$this->msg_type[$v['msg_type']];
        }

        $adminList=model('admin')->field(['id value','realname name'])->where('status',1)->select();
        $adminList=json_encode($adminList,JSON_UNESCAPED_UNICODE);

        return $this->fetch('',[
            'infos' => $infos,
            'phone' => $phone,
            'realname' => $realname,
            'times' => $times,
            'msg_type' => $msg_type,
            'status' => $status,
            'group_id' => $this->group_id,
            'adminList' => $adminList,
            'myaid' => $this->uid,
            'aid' => $aid,
            'msglist' => $this->msg_type,
        ]);
    }



    public function merchant()
    {
        // 查询条件start
        $msg_type = input('msg_type','');
        $times = input('times','');
        $phone = input('phone','');
        $username = input('username','');
        $mer_name = input('mer_name','');
        $mer_desc = input('mer_desc','');
        $status = input('status','');
        $aid = input('aid','');

        $map['role_type'] = 1;

        if(!empty($aid)){
            $map['a_id']=$aid;
        }
        if($this->group_id!=1){
            $map['msg_type']=1;
            $map['a_id']=0;
        }else{

            if(!empty($phone)){
                $aId=model('Merchant')->where('phone',$phone)->value('id');
                if($aId){
                    $map['a_id']=$aId;
                }else{
                    $map['a_id']=0;
                }
            }
            if(!empty($username)){
                $aId=model('Merchant')->where('username',$username)->value('id');
                if($aId){
                    $map['a_id']=$aId;
                }else{
                    $map['a_id']=0;
                }
            }
            if(!empty($mer_name)){
                $aId=model('Merchant')->where('mer_name',$mer_name)->value('id');
                if($aId){
                    $map['a_id']=$aId;
                }else{
                    $map['a_id']=0;
                }
            }
            if(!empty($mer_desc)){
                $aId=model('Merchant')->where('mer_desc',$mer_desc)->value('id');
                if($aId){
                    $map['a_id']=$aId;
                }else{
                    $map['a_id']=0;
                }
            }
        }

        if(!empty($msg_type)){
            $map['msg_type']=$msg_type;
        }

        if($status!=''){
            $map['a.status']=$status;
        }

        if($times != ''){
            $time_arr = explode(' - ',$times);
            $map['a.create_time'] = ['between',[strtotime($time_arr[0]),strtotime($time_arr[1])]];
        }

        $infos = model('MsgNotice')->alias('a')->field('a.*,b.phone,b.mer_name,b.mer_desc,b.username')
            ->join('merchant b','b.id=a.a_id')
            ->where($map)->order('a.id desc')->paginate();

        foreach ($infos as &$v){
            $v['msg_type']=$this->msg_type[$v['msg_type']];
        }

        $adminList=model('Merchant')->field('id,mer_name,username,phone')->where('status',1)->select();
        $merList=[];
        foreach ($adminList as $v){
            $merList[]=['value'=>$v['id'],'name'=>$v['mer_name'].'【账号'.$v['username'].'】(联系电话'.$v['phone'].')'];
        }
//        $adminList=json_encode($adminList,JSON_UNESCAPED_UNICODE);
        $merList=json_encode($merList,JSON_UNESCAPED_UNICODE);

        return $this->fetch('',[
            'infos' => $infos,
            'phone' => $phone,
            'username' => $username,
            'mer_name' => $mer_name,
            'mer_desc' => $mer_desc,
            'times' => $times,
            'msg_type' => $msg_type,
            'status' => $status,
            'group_id' => $this->group_id,
            'adminList' => $merList,
            'msglist' => $this->msg_type,
        ]);
    }


    public function save()
    {
        if(request()->isAjax()){
            try{

                $id = input('post.id');
                $save_data = input('post.');

                if(empty($save_data['msg_type'])){
                    return self::showReturnCode(1011,'请选消息类型');
                }
                if(empty($save_data['admin_id'])){
                    return self::showReturnCode(1011,'请选择接收用户');
                }

                $adminArr=explode(',',$save_data['admin_id']);
                $insertData=[];
                foreach ($adminArr as $v){
                    foreach ($save_data['msg_type'] as $k=>$vv){
                        if($k==4){
                            $insertData[]=['role_type'=>2,'msg_type'=>$k,'a_id'=>$v,'title'=>$save_data['title'],'content'=>$save_data['content'],'status'=>1];
                        }else{
                            $insertData[]=['role_type'=>2,'msg_type'=>$k,'a_id'=>$v,'title'=>$save_data['title'],'content'=>$save_data['content']];
                        }
                        if($k==4){//发送短信
                            $sendPhone=Db::name('admin')->where('id',$v)->value('tel');
                            if(!empty($sendPhone)&&strlen($sendPhone)==11){
                                $mobiles=$sendPhone;
                                $content='【'.config('database')['host_sms_name'].'】'.$save_data['content'];
                                $extno='';
                                $sendtime='';
//                                $res=MessageSend::send($mobiles,$extno,$content,$sendtime);
                                $res=SmsMuSend::send($mobiles,$content);
                            }
                        }
                    }
                }

                $result = model('MsgNotice')->saveAll($insertData);
                $this->RecordSystemLog($this->uid,$this->behavior,'消息发送成功');

                return self::showReturnCode(1001,'消息发送成功');
            }catch (\Exception $e){
                Log::error($e->getMessage());
                return self::showReturnCode(1011,$e->getMessage());
            }
        }else{
            return self::showReturnCode(1002);
        }
    }

    //商户消息
    public function save2()
    {
        if(request()->isAjax()){
            try{

                $id = input('post.id');
                $save_data = input('post.');

                if(empty($save_data['msg_type'])){
                    return self::showReturnCode(1011,'请选消息类型');
                }
                if(empty($save_data['admin_id'])){
                    return self::showReturnCode(1011,'请选择接收用户');
                }

                $adminArr=explode(',',$save_data['admin_id']);
                $insertData=[];
                foreach ($adminArr as $v){
                    foreach ($save_data['msg_type'] as $k=>$vv){
                        if($k==4){
                            $insertData[]=['role_type'=>1,'msg_type'=>$k,'a_id'=>$v,'title'=>$save_data['title'],'content'=>$save_data['content'],'status'=>1];
                        }else{
                            $insertData[]=['role_type'=>1,'msg_type'=>$k,'a_id'=>$v,'title'=>$save_data['title'],'content'=>$save_data['content']];
                        }
                        if($k==4){//发送短信
                            $sendPhone=Db::name('Merchant')->where('id',$v)->value('phone');
                            if(!empty($sendPhone)&&strlen($sendPhone)==11){
                                $mobiles=$sendPhone;
                                $content='【'.config('database')['host_sms_name'].'】'.$save_data['content'];
                                $extno='';
                                $sendtime='';
//                                $res=MessageSend::send($mobiles,$extno,$content,$sendtime);
                                $res=SmsMuSend::send($mobiles,$content);
                            }
                        }
                    }
                }

                $result = model('MsgNotice')->saveAll($insertData);
                $this->RecordSystemLog($this->uid,$this->behavior,'消息发送成功');

                return self::showReturnCode(1001,'消息发送成功');
            }catch (\Exception $e){
                Log::error($e->getMessage());
                return self::showReturnCode(1011,$e->getMessage());
            }
        }else{
            return self::showReturnCode(1002);
        }
    }

    public function status()
    {
        if($this->request->isAjax()){
            $id = input('post.id');
            $res = model('MsgNotice')->save(['status'=>input('post.status')],['id'=>$id]);
            if($res){
                $this->RecordSystemLog($this->uid,$this->behavior,'状态操作成功,操作ID: '.$id);
                return $this->showReturnCode(1001);
            }else{
                return $this->showReturnCode(1009,'操作失败');
            }
        }else{
            return $this->showReturnCode(1002);
        }
    }

    public function delete()
    {
        if($this->request->isAjax()){
            $id = input('post.id');
            $model = 'MsgNotice';

            $res = model($model)->destroy($id);
            if($res){
                $this->RecordSystemLog($this->uid,$this->behavior,'删除成功,操作ID: '.$id);
                return $this->showReturnCode(1001);
            }else{
                return $this->showReturnCode(1009,'删除失败');
            }
        }else{
            return $this->showReturnCode(1002);
        }
    }
}